import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Index from '../views/index/index.vue'
import Login from '../views/login/index.vue'
import Error from '../views/common/error/index.vue'
import UserInfo from '../views/common/usercenter/UserInfo.vue'
import ChangePwd from '../views/common/usercenter/ChangePwd.vue'
import ReportDataList from '../views/data/list/index.vue'
import { useUserStore } from '../stores/user.js';
const modules = import.meta.glob('@/views/**/*.vue')


const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: Home,
            redirect: '/index',
            children: [
                {
                    path: '/index', name: 'index', component: Index, meta: { name: '首页' }
                },
                {
                    path: '/user-info', name: 'user-info', component: UserInfo, meta: { name: '个人中心' }
                },
                {
                    path: '/change-pwd', name: 'change-pwd', component: ChangePwd, meta: { name: '修改密码' }
                },
                {
                    path: '/report-data-list/:id', name: 'report-data-list', component: ReportDataList, meta: { name: '报表数据列表' }
                },
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: Login,
        },
        {
            path: '/error',
            name: 'error',
            component: Error,
        },

    ],
})

router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore();
    let token = userStore.token
    if (!token) {
        token = localStorage.getItem("token")
        if (token) {
            userStore.setToken(token)
        }
    }
    if (!token) {
        if (to.path !== '/login') {
            return next('/login')
        }
        return next()
    }
    if (!userStore.isLoadInfo) {
        const success = await userStore.loadInfo()
        if (success) {
            let menus = userStore.menus
            // console.log("menus", menus)
            addDynamicRouter(menus)
            return next({ ...to, replace: true })
        } else {
            return next('/error')
        }
    }
    next()
})

const addDynamicRouter = (menus) => {
    if (menus && menus.length > 0) {
        menus.forEach(item => {
            if (item.type.code != 'EXPAND') {
                let route = {
                    path: item.path,
                    name: item.name,
                    component: modules[`/src/views/${item.component}.vue`],
                    meta: {
                        name: item.name
                    }
                }
                router.addRoute("home", route)
            }
            addDynamicRouter(item.children)
        })
    }
}

const dynamicImport = (viewPath) => {
    const fullPath = `/src/views/${viewPath}.vue`
    if (views[fullPath]) {
        return views[fullPath]
    }
    return () => Index
}

export default router
